

public class LinkedListStack <E> implements Stack<E>{
    private LinkedList<E> linkedList;

    public LinkedListStack() {
        this.linkedList = new LinkedList<>();
    }

    @Override
    public void push(E e) {
        linkedList.addFirst(e);
    }

    @Override
    public E pop() {
        return linkedList.removeFirst();
    }

    @Override
    public E peek() {
        return linkedList.getFirst();
    }

    @Override
    public int getSize() {
        return linkedList.getSize();
    }

    @Override
    public boolean isEmpty() {
        return linkedList.isEmpty();
    }

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder("Stack: top ");
        sb.append(linkedList);
        sb.append(String.format(" size = %s",getSize()));
        return sb.toString();
    }

    public static void main(String[] args) {
        //Stack<Integer> stack = new ArrayStack<>();
        LinkedListStack<Integer> stack = new LinkedListStack<>();
        for (int i = 0; i < 5; i++) {
            stack.push(i);
            System.out.println(stack);
        }
        stack.pop();
        stack.pop();
        stack.pop();
        stack.pop();
        System.out.println(stack);
        stack.pop();
        System.out.println(stack);
    }
}
